package com.sh.suhe.utildemos.utils.socket.entity;

import com.sh.suhe.utildemos.model.Message;
import com.sh.suhe.utildemos.utils.JsonUtil;
import com.sh.suhe.utildemos.utils.LogUtil;



public class RequestPacket {
    private static final String sCharset = "UTF-8";
    Object data ;
    byte[] buffer;
    public RequestPacket(Object reqData){
//        Gson
        data = reqData;
    }

    public byte[] encode() {
        try {
            String str = JsonUtil.object2Json(data);
            buffer =  str.getBytes(sCharset);
        }catch (Exception e){
            LogUtil.e("RequestPacket","数据解析错误"+ e.getLocalizedMessage());
        }
        if (buffer != null){
            return buffer;
        }
        return new byte[0];
    }

    public void setRequestId(int iRequestId) {
        if (data != null && data instanceof Message){
            ((Message) data).setReqId(iRequestId);
        }
    }
}
